﻿using OASystem.BLL;
using OASystem.BLL.IService;
using OASystem.Model;
using OASystem.Models;
using OASystem.ViewModel;
using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace OASystem.Controllers
{
    public class LoginController : Controller
    {
        //调用用户表的业务层
        UserInfoService adminUserService = new UserInfoService();
        //统一业务层responseResult的返回值获取数据
        ResponseResult responseResult = new ResponseResult();
        //调用管理表的业务层
        SelectListService modueService = new SelectListService();
        //判断是否成功
        Operate operateResult = new Operate();
        //调用此类读取管理模块的列表信息并且显示出来
        PageListResult<Trainlist> resultDataSet = new PageListResult<Trainlist>();



        //UserInfo表的注入
        private ILoginService _loginService;
        //培训管理表的注入
        private ITrainService _trainService;
        public LoginController(ILoginService loginService, ITrainService trainService)
        {
            _loginService = loginService;
            _trainService = trainService;
        }

        // GET: Login
        #region Index页面
        public ActionResult Index()
        {
            Expression<Func<Userinfo, bool>> lambdaExpression = a => true;
            ViewBag.a = _loginService.QueryList(lambdaExpression);
            return View();
        }
        #endregion

        #region 登陆操作
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public JsonResult Query(Userinfo users)
        {
           //调用用户对象
            Userinfo user = new Userinfo();
            //调用业务层的lamdam表达式的记住密码
            responseResult = adminUserService.AdminByReader(users);
            user = responseResult.data;

            //跟上面的MD5判断数据库是否存在重复了 user = adminUserService.AdminByReader(users);
            //Expression<Func<Userinfo, bool>> lambdaExpression = a => a.UserName == user.UserName && a.Password == user.Password;
            //responseResult.success = adminUserService.Query(lambdaExpression) != null;

            if (user !=null)
            {
                //创建cookie对象
                HttpCookie httpCookie = new HttpCookie("CookieName");
                httpCookie.Values.Add("UserId", user.UserId.ToString());
                httpCookie.Values.Add("UserName", user.UserName);
                httpCookie.Values.Add("Password", user.Password);
                httpCookie.Values.Add("DepartId", user.DepartId.ToString());
                httpCookie.Values.Add("LastLoginTime", user.LastLoginTime.ToString());
                httpCookie.Values.Add("Enabled", user.Enabled.ToString());
                httpCookie.Values.Add("Status", user.Status.ToString());
                httpCookie.Values.Add("EmployeeName", user.EmployeeName);
                httpCookie.Values.Add("Address", user.Address);
                httpCookie.Values.Add("LastLoginIP", user.LastLoginIP);
                httpCookie.Values.Add("Remark", user.Remark);
                httpCookie.Values.Add("CreateTime", user.CreateTime.ToString());
                //设置过期时间
                httpCookie.Values.Add("time", DateTime.Now.AddDays(7).ToString());
                //添加cookie对象
                System.Web.HttpContext.Current.Response.Cookies.Add(httpCookie);
                responseResult.success = true;
                //给上下文赋值
                AdminContext.context.adminUser = user;
            }
            return Json(responseResult);
        }
        #endregion

        #region 去除session和cookie退出登录
        /// <summary>
        /// 去除session和cookie
        /// </summary>
        /// <returns></returns>
        public ActionResult AdminOut()
        {
            //清除session
            AdminContext.context.adminUser = null;
            //获取cookie
            HttpCookie cok = Response.Cookies["CookieName"];
            if (cok != null)
            {
                cok.Values.Clear();

            }
            return Redirect("/Home/Login");
        }
        #endregion

        #region 培训管理列表显示
        /// <summary>
        /// 我的培训显示信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetMoudelList2(int page, int limit, string title, string speaker, string date)
        {
            resultDataSet.code = 0;
            resultDataSet.msg = string.Empty;
            int count = 0;
            Expression<Func<Trainlist, int>> orderbyLambda = a => a.Id;
            responseResult = modueService.GetMoudelList(page, limit, AdminContext.context.adminUser.UserId, title, speaker, date, orderbyLambda, out count);
            resultDataSet.data = responseResult.data;
            resultDataSet.count = count;
            return Json(resultDataSet, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 根据登录用户显示登陆用户的信息
        /// <summary>
        /// 培训管理显示信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetMoudelList(int page, int limit,  string title, string speaker, string dates)
       {
         
            resultDataSet.code = 0;
            resultDataSet.msg = string.Empty;
            int count = 0;         
            Expression<Func<Trainlist, int>> orderbyLambda = a => a.Id;
            responseResult = modueService.TrainGetList(page,limit,title, speaker, dates, orderbyLambda, out count);
            resultDataSet.data = responseResult.data;
            resultDataSet.count = count;
            return Json(resultDataSet, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 单条数据删除按钮
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="infoManager"></param>
        /// <returns></returns>
        public JsonResult DELETE(Trainlist infoManager)
        {
            operateResult.Success = modueService.DELETE(infoManager);
            return Json(operateResult);
        }
        #endregion

        #region 添加信息按钮
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="infoManager"></param>
        /// <returns></returns>
        public JsonResult Add(Trainlist infoManager)
        {      
            operateResult.Success = modueService.Add(infoManager);
            return Json(operateResult);
        }
        #endregion

        #region 我的培训编辑按钮
        /// <summary>
        /// 我的培训编辑功能
        /// </summary>
        /// <param name="infoManager"></param>
        /// <returns></returns>
        public JsonResult Update(Trainlist infoManager)
        {
            operateResult.Success = modueService.Update(infoManager);
            return Json(operateResult);
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 根据id删除多个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult BatchDelete(string id)
        {
            //string转换string[]数据
            string[] strArray = id.Split(',');
            //string转换int[]
            int[] iNums = Array.ConvertAll(strArray, int.Parse);
            operateResult.Success = _trainService.BatchDeleteById(iNums);
            return Json(operateResult);
        }
        #endregion

        #region 首页修改密码
        public JsonResult IndexUpdate(Userinfo users,string newpwd)
        {
            //把加密新密码存入Session中
            users.UserId = AdminContext.context.adminUser.UserId;
            responseResult = adminUserService.SelectbyUpdate(users, newpwd);
            operateResult.Success = responseResult.success;
            return Json(operateResult);
        }
        #endregion

        
    }
}

